<template>
  <!-- 预警级别设置 -->
  <div>
    <div style="height: 85vh;overflow: auto">

      <div class="phone-class">
        <span class="phone-title">各项监测预警阈值设定</span>
      </div>
      <div style="padding: 0px 20px">
        <el-switch
          style="display: block;margin-bottom: 20px"
          v-model="flag"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="启用预警规则"
          inactive-text="停用预警规则"
          @change="changeFlag">
        </el-switch>
        <el-tabs v-model="activeName" tab-position="left" type="border-card">
          <el-tab-pane v-for="item in warningRules" :key="item.id" :label="item.monitorTypeName"
                       :name="item.monitorTypeName">
            <el-button  style="margin-bottom: 20px" @click="updateWarning(item)" size="small"
                       icon="el-icon-folder-checked"
                       type="primary" class="phone-btn" :disabled="flag==0">
              保存
            </el-button>
            <el-table :data="item.warningLevelList" border>
              <el-table-column label="序号" fixed width="65">
                <template slot-scope="scope">
                  {{getIndex(scope.$index)}}
                </template>
              </el-table-column>
              <el-table-column prop="warningLevelName" label="预警等级" min-width="90">
              </el-table-column>
              <el-table-column prop="threshold" :label="item.monitorType | threTitle" width="150">
                <template slot-scope="scope">
                  <el-input size="mini" v-model="scope.row.threshold" :disabled="scope.row.warningLevelName==='正常'"
                            placeholder="请输入预警值"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="validFlag" label="启用预警" width="150">
                <template slot-scope="scope">
                  <el-radio-group v-model="scope.row.validFlag" size="mini">
                    <el-radio-button label=1>启用</el-radio-button>
                    <el-radio-button label=0>停用</el-radio-button>
                  </el-radio-group>
                </template>
              </el-table-column>
              <el-table-column prop="updateTime" label="更新时间" width="170">
                <template>
                  {{item.updateTime|formatTimer()}}
                </template>
              </el-table-column>
              <el-table-column prop="operator" label="操作人" width="120">
                <template>
                  {{item.operator}}
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>

<script>
import {
  warningRuleList,
  warningRuleUpdate,
  warningUserUpdate,
  queryUserByProject,
  warningRuleFlag
} from '@/services/model/sensor'
import Format from '@/utils/format'
import Enums from '@/utils/enums'
import Permission from '@/utils/permission'
export default {
  mixins:[Enums],
  data() {
    return {
      activeName: '倾角仪',
      form: {
        workPointId: ''
      },
      total: 0,
      tableList: [],
      updateForm: {},
      phoneInfo: {
        userIdList: [],
        workPointId: ''
      },
      flag: false,
      warningRules: [],
      isShowDialog: false,
      warningTitle: '',
      userSelectList: [],
      EnumWarningLevelList: [],
      showUserSelect: false,
    }
  },
  filters: {

    // 时间格式化
    formatTimer(timer) {
      return Format(new Date(timer))
    },
    // 修改表头
    threTitle(monitorType) {
      switch (monitorType) {
        case 8 :
          return '倾角(θ ≥ Degree)'// 倾角
        case 6 :
          return '应力(F < % * 初始值)'// 应力
        case 4 :
          return '降雨量（>=mm/h)'
        default:
          return '位移(X ≥ Ymm)'
      }
    }
  },
  methods: {


    async changeFlag() {
      this.updateForm.flag = this.flag ? 1 : 0
      let res = await warningRuleFlag(this.updateForm)
      this.queryList()

    },

    // 获取当前的序号
    getIndex(index) {
      let param = index + 1
      return param
    },
    // 预警列表
    changeWorkPointId(workPointId){
      const projectId = this.$route.params?.projectId
      this.form.workPointId = workPointId
      this.form.projectId = projectId
      this.queryList()
    },
    async queryList() {
      let res = await warningRuleList(this.form)
      this.warningRules = res.data || []
      this.phoneInfo.userIdList = this.warningRules[0].userIdList || []
      this.flag = this.warningRules[0].flag === 1 ? true : false;
      this.updateForm.workPointId = this.form.workPointId;
      this.total = Number(res.data?.total)
    },
    // 修改
    updateWarning(item) {
      this.updateForm = item
      this.updateForm.workPointId = this.form.workPointId
      this.submitData()
    },
    // 修改
    async submitData() {
      let res = await warningRuleUpdate(this.updateForm)
      if (res.code === 200) {
        this.$notify({
          type: 'success',
          title: '提示',
          message: '修改成功'
        })
        this.queryList(this.form.workPointId)
        return true
      }
    },
  },
  activated() {
    this.queryList()
  },
  mounted() {
    this.queryDictList(['EnumWarningLevel'], ['EnumWarningLevelList'])
  },
}
</script>
<style scoped="scoped" lang="less">
  .page {
    .detail-header {
      color: #1990FE;
    }

  }

  .s-phone {
    width: 150px;
  }

  .warning-header {
    margin-bottom: 10px;
    color: #606266;
    height: 30px;
    line-height: 30px;
  }

  .warning-header h2 {
    font-size: 16px;
    font-weight: bold;
  }

  .s-green {
    color: #2dca7f;
  }

  .s-red {
    color: #f35e5f;
  }

  /deep/ .el-table__header .el-table-column--selection .cell .el-checkbox:after {
    content: " 是否预警";
    color: #909399;
    font-size: 13px;
  }

  .phone-class {
    margin: 10px 0 10px 40px;
    border-bottom: 2px solid #1990FE !important;

    span {
      color: white
    }

    .phone-btn {
      background: #1990FE;
      width: 80px;
      text-align: center;
      border-color: transparent
      // height: 20px
      // color
    }
  ;
  }

  .phone-class .phone-title {
    color: #1990FE;

    font-size: 18px;
    font-weight: 600;
  }

  .phone-class .phone-info {
    color: rgb(81, 85, 99);
    font-size: 15px;
    margin-left: 5px;
  }

  .addNotifier {
    // background-color: red !important;
  }

  .sms_view {
    /deep/ .el-table td, .el-table th {
      padding: 4px 0;
      font-size: 13px;
    }
  }
</style>
